﻿using System;
using System.Collections.Generic;

namespace _5E.Infrastructure.Domain
{
    public class EntityBase<TId>
    {
        private List<BusinessRule> m_BrokenRules = new List<BusinessRule>();
        public virtual IEnumerable<BusinessRule> BrokenRules
        {
            get
            {
                m_BrokenRules.Clear();
                Validate();
                return m_BrokenRules;
            }
        }

        private TId m_Id;
        public virtual TId Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        protected virtual void Validate() { }

        protected void AddBrokenRule(BusinessRule rule)
        {
            m_BrokenRules.Add(rule);
        }

        public override bool Equals(object obj)
        {
            return obj != null && obj is EntityBase<TId> && this == (EntityBase<TId>)obj;
        }

        public override Int32 GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(EntityBase<TId> entity1, EntityBase<TId> entity2)
        {
            if (null == (object)entity1 && null == (object)entity2)
                return true;

            if (null == (object)entity1 || null == (object)entity2)
                return false;

            if (entity1.Id.ToString() == entity2.Id.ToString())
                return true;

            return false;
        }

        public static bool operator !=(EntityBase<TId> entity1, EntityBase<TId> entity2)
        {
            return (!(entity1 == entity2));
        }
    }
}
